samsa 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 50ecc8c261ebb8875aac4b5613992b786647dbe98df2d156b4ee437592419b26
4
+ data.tar.gz: c927929290f7c4697479e7457426eed9137cf75d5e538669fff00eecedafa18b
5
+ SHA512:
6
+ metadata.gz: 618304b0fa77b181aa8dda3ccfe63fceb8a00d0a954db59356c11edf34f72a9bebabdcc474c5216d39a8ef4ddaefff8d9b5665ed3df9a7b7fb3739c097303014
7
+ data.tar.gz: 6fb83823fd35a959400e3b4f36106ace921427cd84837d28478cade4b666101835029b1b6e044f21ef031880a67c86860616dd16b7c1644167e255046a8b1a61
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 2.0.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at ara@hacopian.de. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in samsa.gemspec
4
+ gemspec
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ samsa (0.1.0)
5
+ ramda-ruby (~> 0.16.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ rake (10.5.0)
12
+ ramda-ruby (0.16.0)
13
+ rspec (3.8.0)
14
+ rspec-core (~> 3.8.0)
15
+ rspec-expectations (~> 3.8.0)
16
+ rspec-mocks (~> 3.8.0)
17
+ rspec-core (3.8.1)
18
+ rspec-support (~> 3.8.0)
19
+ rspec-expectations (3.8.4)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.8.0)
22
+ rspec-mocks (3.8.1)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-support (3.8.2)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 2.0)
32
+ rake (~> 10.0)
33
+ rspec (~> 3.0)
34
+ samsa!
35
+
36
+ BUNDLED WITH
37
+ 2.0.1
@@ -0,0 +1,37 @@
1
+ # Samsa
2
+
3
+ DSL for creating classes that transform hashes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'samsa'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install samsa
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ahacop/samsa. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
34
+
35
+ ## Code of Conduct
36
+
37
+ Everyone interacting in the Samsa project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ahacop/samsa/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "samsa"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,61 @@
1
+ require "samsa/version"
2
+ require "samsa/mapper"
3
+
4
+ module Samsa
5
+ def self.included(base)
6
+ base.class_eval do
7
+ extend ClassMethods
8
+ class_attribute :_definitions
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ def transforms(&definitions)
14
+ self._definitions = definitions
15
+ end
16
+
17
+ def transform(input)
18
+ t = Transforms.new
19
+ t.instance_eval(&_definitions)
20
+ t.run_lazy(input)
21
+
22
+ Samsa::Mapper.new(t.transforms, input)
23
+ .normalize
24
+ .with_indifferent_access
25
+ end
26
+ end
27
+
28
+ class Transforms
29
+ def initialize
30
+ @transforms = []
31
+ @lazy_transforms = []
32
+ end
33
+
34
+ attr_reader :transforms
35
+
36
+ def run_lazy(input)
37
+ @lazy_transforms.each { |t| t.call(input) }
38
+ end
39
+
40
+ def map(from:, to:, default: nil)
41
+ transform = { type: :map, from: extract_path(from), to: extract_path(to) }
42
+ transform[:default] = default unless default.nil?
43
+ @transforms << transform
44
+ end
45
+
46
+ def set(value, to:)
47
+ @transforms << { type: :set, value: value, to: extract_path(to) }
48
+ end
49
+
50
+ def conditional(inputs)
51
+ @lazy_transforms << lambda do |input|
52
+ yield(*Array[*inputs].map { |i| input.dig(*extract_path(i)) })
53
+ end
54
+ end
55
+
56
+ def extract_path(path_str)
57
+ path = path_str.split(".").map { |s| s.to_i.to_s == s ? s.to_i : s.to_sym }
58
+ Array[*path]
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,62 @@
1
+ require "ramda"
2
+
3
+ module Samsa
4
+ class Mapper
5
+ def initialize(template, input)
6
+ @template = template
7
+ @input = input
8
+ end
9
+
10
+ def normalize
11
+ @template.inject({}) do |memo, transform|
12
+ Transform.build(transform).apply(@input, memo)
13
+ end
14
+ end
15
+
16
+ class Transform
17
+ def initialize(transform)
18
+ @transform = transform
19
+ @r = Ramda
20
+ end
21
+
22
+ def self.build(transform)
23
+ case transform[:type]
24
+ when :set then SetTransform.new(transform)
25
+ when :map then MapTransform.new(transform)
26
+ else raise InvalidTransformType
27
+ end
28
+ end
29
+
30
+ protected
31
+
32
+ attr_reader :r
33
+
34
+ class SetTransform < Transform
35
+ def apply(_input, output)
36
+ to_lens = r.lens_path(@transform[:to])
37
+ r.set(to_lens, @transform[:value], output)
38
+ end
39
+ end
40
+
41
+ class MapTransform < Transform
42
+ def apply(input, output)
43
+ value = new_value(input).nil? ? @transform[:default] : new_value(input)
44
+ return output if value.nil?
45
+
46
+ to_lens = r.lens_path(@transform[:to])
47
+ r.set(to_lens, value, output)
48
+ end
49
+
50
+ private
51
+
52
+ def new_value(input)
53
+ return @transform[:default] if @transform[:from].nil?
54
+ from_lens = r.lens_path(@transform[:from])
55
+ r.view(from_lens, input)
56
+ end
57
+ end
58
+ end
59
+
60
+ class InvalidTransformType < StandardError; end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module Samsa
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,41 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "samsa/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "samsa"
7
+ spec.version = Samsa::VERSION
8
+ spec.authors = ["Ara Hacopian"]
9
+ spec.email = ["ara@hacopian.de"]
10
+
11
+ spec.summary = %q{DSL for transforming hashes.}
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/ahacop/samsa"
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
19
+
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/ahacop/samsa"
22
+ spec.metadata["changelog_uri"] = "https://github.com/ahacop/samsa/blob/master/CHANGELOG.md"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_development_dependency "bundler", "~> 2.0"
38
+ spec.add_development_dependency "rake", "~> 10.0"
39
+ spec.add_development_dependency "rspec", "~> 3.0"
40
+ spec.add_dependency "ramda-ruby", "~> 0.16.0"
41
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: samsa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ara Hacopian
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: ramda-ruby
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.16.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.16.0
69
+ description: DSL for transforming hashes.
70
+ email:
71
+ - ara@hacopian.de
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/samsa.rb
86
+ - lib/samsa/mapper.rb
87
+ - lib/samsa/version.rb
88
+ - samsa.gemspec
89
+ homepage: https://github.com/ahacop/samsa
90
+ licenses: []
91
+ metadata:
92
+ allowed_push_host: https://rubygems.org
93
+ homepage_uri: https://github.com/ahacop/samsa
94
+ source_code_uri: https://github.com/ahacop/samsa
95
+ changelog_uri: https://github.com/ahacop/samsa/blob/master/CHANGELOG.md
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubygems_version: 3.0.3
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: DSL for transforming hashes.
115
+ test_files: []